package com.slz.crm.server.controller;

import com.slz.crm.common.annotation.RequirePermission;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.result.Result;
import com.slz.crm.pojo.dto.SalesOpportunityDTO;
import com.slz.crm.pojo.dto.SalesOpportunityQueryDTO;
import com.slz.crm.pojo.dto.SalesStageApprovalDTO;
import com.slz.crm.pojo.vo.SalesOpportunityVO;
import com.slz.crm.common.result.Result;
import com.slz.crm.pojo.dto.SalesOpportunityDTO;
import com.slz.crm.pojo.vo.SalesOpportunityVO;
import com.slz.crm.server.service.SalesOpportunityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售机会
 */
@RestController
@RequestMapping("/sales")
@Slf4j
public class SalesOpportunityController {

    @Autowired
    private SalesOpportunityService salesOpportunityService;


    /**
     * 修改销售机会
     * @param salesOpportunityDTO 销售机会DTO
     * @return 是否修改成功
     */
    @PutMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "UPDATE_SALE_OPPORTUNITY"
    )
    public Result<Boolean> update(@RequestBody SalesOpportunityDTO salesOpportunityDTO){
        return Result.success(salesOpportunityService.update(salesOpportunityDTO));
    }

    /**
     * 删除销售机会
     * @param id 销售机会ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "DELETE_SALE_OPPORTUNITY"
    )
    public Result<Boolean> delete(@PathVariable("id") Long id){
        try {
            return Result.success(salesOpportunityService.delete(id));
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有销售机会
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 销售机会VO列表
     */
    @GetMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "VIEW_SALE_OPPORTUNITY"
    )
    public Result<Page<SalesOpportunityVO>> getAllSalesOpportunity(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        if (pageNum == null || pageSize  == null || pageNum <= 0 || pageSize <= 0) {
            pageNum = 1;
            pageSize = 10;
        }
        return Result.success(salesOpportunityService.getAllSalesOpportunity(pageNum, pageSize));
    }
    /**
     * 创建销售机会
     * @param dto 销售机会DTO
     * @return 销售机会VO
     */
    @PostMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "CREATE_SALE_OPPORTUNITY"
    )
    public Result<SalesOpportunityVO> create(@RequestBody SalesOpportunityDTO dto) {
        return Result.success(salesOpportunityService.create(dto));
    }

    /**
     * 自定义查询销售机会
     * @param queryDTO 查询条件
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 销售机会VO列表
     */
    @GetMapping("/query")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "CUSTOM_QUERY_SALE_OPPORTUNITY"
    )
    public Result<Page<SalesOpportunityVO>> getSalesOpportunityByQuery(@ModelAttribute SalesOpportunityQueryDTO queryDTO, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return Result.success(salesOpportunityService.getSalesOpportunityByQuery(queryDTO, pageNum, pageSize));
    }


}
